package com.google.project.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.project.client.Service.AssignTaskService;
import com.google.project.server.entity.AssignTask;
import com.google.project.server.entity.ProjectTask;
import com.google.project.shared.AssignTaskDTO;
import com.google.project.shared.ProjectStaffDTO;

@SuppressWarnings("serial")
public class AssignTaskServiceImpl extends RemoteServiceServlet implements
		AssignTaskService {
	
	
	@Override
	public void addStaff(List<ProjectStaffDTO> listStaff,String taskId)
	{
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			for(ProjectStaffDTO staff:listStaff)
			{
				if(staff.getPosition()==3)
				{
					ProjectTask task = pm.getObjectById(ProjectTask.class,taskId);
					task.setOptUserId(staff.getUserId());
					task.setOptAccountName(staff.getUserName());
				}
			
				AssignTask assign = new AssignTask();
				assign.setTaskId(taskId);
				assign.setUserId(staff.getUserId());
				assign.setUserCode(staff.getUserCode());
				assign.setUserName(staff.getUserName());
				assign.setSpecializeName(staff.getSpecializeName());
				assign.setExperient(staff.getExperient());
				pm.makePersistent(assign);
				
			}
		}catch(Exception e)
		{}finally 
		{pm.close();}
	
	}
	@Override
	public List<AssignTaskDTO>  getStaffOfTask(String taskId)
	{
		List<AssignTaskDTO> result = new ArrayList<AssignTaskDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		try{
			Query q = pm.newQuery(AssignTask.class,"taskId==\""+taskId+"\"");
			List<AssignTask> list = (List<AssignTask>) q.execute();
			for (AssignTask staff : list) {
				result.add(staff.toDTO());
			}
	   
	}catch(Exception e)
	{
		System.out.println(e.getMessage());
	}finally 
	{pm.close();}
		return result;
	}

}
